package br.sed.theenemy.business.services;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 
 * @author Paulo Mendes
 * @email paulo.mendes87@gmail.com
 * 
 * This class send the notifications of application. 
 *
 */
public class Notifier{
	private String host; 			// = "yourmailhost";
	private String to; 				// = request.getParameter("to");
	private String from; 			// = request.getParameter("from");
	private String subject; 		// = request.getParameter("subject");
	private String messageText;		//  = request.getParameter("body");
	private boolean sessionDebug; 	//= false;
	
	public Notifier(){
		this.host = "localhost";
		this.sessionDebug = false;
	}
	
	public void sendRequestUserMail(String to, String hash, String hostName) throws AddressException, MessagingException{		
		
		//String host = "http://localhost:8080/the-enemy/";
		//TODO: Install Tomcat and request.getServerName() to host
		String host = "http://"+hostName+":8080/the-enemy/";
		this.to = to;
		this.from = "general@enemy.com";
		this.subject = "[Enemy] New User Request";
		
		this.messageText = "<h2>Welcome to Enemy</h2> " +
						   "<p>Para completar o seu cadastro basta clicar no link abaixo: </p>" +
						   "<p><a href="+host+"activeUser?hash="+hash+">Active User Link</a></p>";
		this.send();
	}
	public void setParameters(String to, String from, String subject){
		this.to = to;
		this.from = from;
		this.subject = subject;
	}
	public void setMessage(String messageText){
		this.messageText = messageText;
	}
	// Create some properties and get the default Session.
	public void send() throws AddressException, MessagingException{		 
		Properties props = System.getProperties();
		props.put("mail.host", host);
		props.put("mail.transport.protocol", "smtp");
		Session mailSession = Session.getDefaultInstance(props, null);
		 
		// Set debug on the Session
		// Passing false will not echo debug info, and passing True will.
		mailSession.setDebug(sessionDebug);
		 
		// Instantiate a new MimeMessage and fill it with the 
		// required information.		 
		Message msg = new MimeMessage(mailSession);
		
		InternetAddress[] address = {new InternetAddress(to)};
		msg.setFrom(new InternetAddress(from));
		msg.setRecipients(Message.RecipientType.TO, address);
		msg.setSubject(subject);
		msg.setSentDate(new Date());
		//msg.setText(messageText);
		msg.setContent(messageText, "text/html");
		Transport.send(msg);
		
		//System out para todo o sistema para ser executado, nunca use.
//		System.out.println("Mail was sent to " + to);
//		System.out.println(" from " + from);
//		System.out.println(" using host " + host + ".");
	}
//	public static void main(String[] args) {
//		Notifier n = new Notifier();
//		
//		n.setParameters("paulo.mendes87@gmail.com", "paulo.mendes87@gmail.com", "Message Test");
//		System.out.println("Setting the Parameters");
//		n.setMessage("This is a test message");
//		System.out.println("Settin message");
//		n.send();
//		System.out.println("Sending Mail");
//	}
}
